﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace GlassStore.Helpers
{
    public static class HtmlHelpers
    {
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (string.IsNullOrEmpty(input) || input.Length <= length)
            {
                return input;
            }
            else
            {
                return input.Substring(0, length) + "...";
            }
        }

        public static string Pager(this HtmlHelper helper, int index, int sum, string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("&nbsp;共{0} 页, 当前第 {1} 页&nbsp;", sum, index));
            if (index > 1 && sum > 1)
                sb.Append(string.Format("&nbsp;<a href='{0}{1}'>首页</a>", url, 1));
            if (index > 1)
                sb.Append(string.Format("&nbsp;<a href='{0}{1}'>上一页</a>", url, index - 1));
            for (int i = 1; i <= sum; i++)
            {
                if (i == index)
                    sb.Append(string.Format("&nbsp;<a href='{0}{1}' class='current'>{1}</a>", url, i));
                else
                    sb.Append(string.Format("&nbsp;<a href='{0}{1}'>{1}</a>", url, i));
            }
            if (index < sum)
            {
                sb.Append(string.Format("&nbsp;<a href='{0}{1}'>下一页</a>", url, index + 1));
                sb.Append(string.Format("&nbsp;<a href='{0}{1}'>末页 </a> ", url, sum));
            }
            return sb.ToString();
        }
    }
}